<template>
  <div class="table-container">
    <el-table
      ref="goodsTable"
      :data="goodsList"
      style="width: 100%"
      v-loading="listLoading"
      border
    >
      <el-table-column type="selection" width="60" align="center"></el-table-column>

      <el-table-column label="商品图片" width="200" align="center">
        <template slot-scope="scope">
          <img style="height: 80px" :src="scope.row.imageUrl" :title="scope.row.description" />
        </template>
      </el-table-column>

      <el-table-column label="商品标题" width="200" align="center">
        <template slot-scope="scope">{{scope.row.title}}</template>
      </el-table-column>

      <el-table-column label="价格" width="80" align="center">
        <template slot-scope="scope">{{scope.row.price}}</template>
      </el-table-column>

      <el-table-column label="数量" width="80" align="center">
        <template slot-scope="scope">{{scope.row.amount}}</template>
      </el-table-column>

      <el-table-column label="商品描述" width="200" align="center">
        <template slot-scope="scope">{{scope.row.description}}</template>
      </el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDetails(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getGoodsList } from "@/api/goods";

export default {
  name: "GoodsList",
  props: {
    isUpdate: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      goodsList: null,
      listLoading: true,
    };
  },
  created() {
    if (this.isUpdate) {
      this.getGoodsList();
    } else {
      alert("不进行数据更新");
      this.listLoading = false;
    }
  },
  methods: {
    getGoodsList() {
      this.listLoading = true;
      getGoodsList().then((response) => {
        this.listLoading = false;
        this.goodsList = response.data;
        console.log(this.goodsList);
      });
    },
    /**
     * 此方法用于测试页面带参数跳转
     * 参考资料：https://www.jb51.net/article/160401.htm
     *
     * 方式一：get
     * 发起：this.$router.push({path:'/home',query: {id:'1'}})
     * 接收：this.$route.query.id
     * 优点：可以刷新
     * 缺点：参数暴露在地址栏，不安全
     *
     * 方式二：post
     * 发起： this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
     * 接收：this.$route.params.id
     * 优点：安全
     * 缺点：刷新后参数丢失
     * 解决方法：在created中判断是否接收到参数，如果有，则写入内存，如果没有则不操作。在mounted中读取内存参数，写入页面data
     */
    handleDetails(index, row) {
      this.$confirm("是否要跳转到商品详情", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$router.push({
          name: "goodsdetails",
          params: { info: row.title },
        });
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
